﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HK2009.BusinessFacade;
using HK2009.EntityManage;
using HK2009.Common;
using HK2009.EntityQuery;

namespace HK2009.BusinessFacadeHandler
{
    /// <summary>
    /// 角色权限管理模块现实
    /// </summary>
    public class RolePurviewHandler:IRolePurviewHandler
    {
        PurviewsManage purManage = new PurviewsManage();
        UsersManage userManage = new UsersManage();
        RolesManage roleManage = new RolesManage();
        DataBase db = new DataBase();
        /// <summary>
        /// 获取所有权限列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllPurviewList()
        {
            string[] orderBy = new string[2];
            orderBy[0] = "PurviewGroupCode";
            orderBy[1] = "PurviewID";
          DataTable dt= purManage.GetPurviewsLists(orderBy,true);
          if (dt != null && dt.Rows.Count > 0)
          {
              return dt;
          }
          else
          {
              return null;
          }
        }       

        /// <summary>
        /// 根据sql语句同参数获取权限列表
        /// </summary>
        /// <param name="sqlStr">sql语句</param>
        /// <param name="parms">参数</param>
        /// <returns></returns>
        public DataTable GetPurviewList(string sqlStr, SqlParameter[] parms)
        {
            return db.RunSqlTab(sqlStr, parms);        
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="loginID">LoginID</param>
        /// <param name="password">password</param>
        /// <returns></returns>
        public UsersQuery UserLogin(string loginID, string password)
        {
            SqlParameter[] parms = new SqlParameter[3];
            parms[0] = new SqlParameter("@LoginID",loginID);
            parms[1] = new SqlParameter("@Password",password);
            parms[2] = new SqlParameter("@IsActivity","1");
            UsersQuery[] query = userManage.GetUsersQueryList(" LoginID=@LoginID and Password=@Password and IsActivity=@IsActivity ", parms); ;
            if (query != null && query.Length > 0)
            {
                return query[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据sql语句同参数获取用户列表
        /// </summary>
        /// <param name="sqlStr"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public DataTable GetUsersList(string sqlStr, SqlParameter[] parms)
        {
            return db.RunSqlTab(sqlStr, parms);
        }

        /// <summary>
        /// 根据角色ＩＤ获取角色信息
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public DataTable GetRoles(int roleID)
        {
            DataTable dt = roleManage.GetRoles(roleID);
            return dt;
        }


        /// <summary>
        /// 传入Role实体，更新角色信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool UpdateRoles(RolesQuery query)
        {
           return roleManage.UpdateRoles(query);
        }

        /// <summary>
        /// 传入Role实体，添加角色信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool InsertRoles(RolesQuery query)
        {
          return  roleManage.InsertRoles(query);
        }


        /// <summary>
        /// 根据角色名获取角色信息
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public  DataTable GetRolesList(string roleName)
        {
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = new SqlParameter("@RoleName",roleManage);
          return  roleManage.GetRolesList(" RoleName=@RoleName",parms);
        }

        /// <summary>
        /// 根据sql语句同参数获取角色列表
        /// </summary>
        /// <param name="sqlStr">sql语句</param>
        /// <param name="parms">参数</param>
        /// <returns></returns>
        public DataTable GetRoleList(string sqlStr, SqlParameter[] parms)
        {
            return db.RunSqlTab(sqlStr, parms);        

        }

        /// <summary>
        /// 根据权限ID获取权限信息
        /// </summary>
        /// <param name="purviewID"></param>
        /// <returns></returns>
        public DataTable GetPurview(int purviewID)
        {
            return purManage.GetPurviews(purviewID);
        }

        /// <summary>
        /// 根据roleID删除角色
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public bool RoleDelete(int roleID)
        {
            return roleManage.DeleteRoles(roleID);
        }

        /// <summary>
        /// 根据userID删除用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool UserDelete(int userID)
        {
            return userManage.DeleteUsers(userID);
        }

        /// <summary>
        ///  更改用户信息
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="isActivity"></param>
        /// <returns></returns>
        public bool UserUpdate(UsersQuery query)
        {
            return userManage.UpdateUsers(query);
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool UserInsert(UsersQuery query)
        {
            return userManage.InsertUsers(query);
        }

        /// <summary>
        /// 根据userID获取用户信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public UsersQuery GetUsers(int userID)
        {
            return userManage.GetUsersQuery(userID);
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllRoles()
        {
            return roleManage.GetRolesList();
        }

        public int GetUserIDByLoginID(string loginID)
        {
            int id = 0;
            SqlParameter[] parms = new SqlParameter[1];
            parms[0] = new SqlParameter("@LoginID",loginID);
            DataTable dt = userManage.GetUsersList(" LoginID=@LoginID",parms);
            if (dt != null && dt.Rows.Count > 0)
            {
                id = int.Parse(dt.Rows[0]["UserID"].ToString());
            }
            return id;
        }

    }
}
